package cn.wolfcode.wolf2w.strategy.service.impl;

import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import cn.wolfcode.wolf2w.strategy.api.domain.StrategyRank;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyRankMapper;
import cn.wolfcode.wolf2w.strategy.query.StrategyRankQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyRankService;
import cn.wolfcode.wolf2w.strategy.service.IStrategyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 攻略排行Service业务层处理
 * 
 * @author dafei
 * @date 2023-11-24
 */
@Service
@Transactional
public class StrategyRankServiceImpl extends ServiceImpl<StrategyRankMapper,StrategyRank> implements IStrategyRankService {

    @Autowired
    private IStrategyService strategyService;

    @Override
    public IPage<StrategyRank> queryPage(StrategyRankQuery qo) {
        IPage<StrategyRank> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyRank> queryRankByType(int type) {

//        select * from ta_strategy_rank
//        where type = 3 and statis_time  =
//                (select max(statis_time) from ta_strategy_rank where type = 3)
//        order by statisnum desc
//        limit 10

        return lambdaQuery().eq(StrategyRank::getType, type)
                .inSql(StrategyRank::getStatisTime, "(select max(statis_time) from ta_strategy_rank where type = "+type+")")
                .orderByDesc(StrategyRank::getStatisnum)
                .last("limit 10")
                .list();
    }

    @Override
    public void strategyRankHandle(int type) {
        //1: 使用慢sql 从大表(strategy) 查询数据
        //2：将返回值添加小表(strategy_rank)中
        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        if(StrategyRank.TYPE_HOT == type){
            wrapper.orderByDesc("viewnum + replynum");
        }else if(StrategyRank.TYPE_CHINA == type){
            //国内
            wrapper.eq("isabroad", 0);
            wrapper.orderByDesc("thumbsupnum + favornum");
        }else if(StrategyRank.TYPE_ABROAD == type){
            //国外
            wrapper.eq("isabroad", 1);
            wrapper.orderByDesc("thumbsupnum + favornum");
        }
        wrapper.last(" limit 10");
        List<Strategy> list = strategyService.list(wrapper);
        List<StrategyRank> ranks = new ArrayList<>();
        Date now = new Date();
        for (Strategy strategy : list) {
            StrategyRank rank = new StrategyRank();
            rank.setStrategyId(strategy.getId());
            rank.setStrategyTitle(strategy.getTitle());
            rank.setType(type);
            if(type == StrategyRank.TYPE_HOT){
                rank.setStatisnum(strategy.getViewnum() + strategy.getReplynum() + 0L);
            }else{
                rank.setStatisnum(strategy.getThumbsupnum() + strategy.getFavornum() + 0L);
            }
            rank.setStatisTime(now);
            rank.setDestId(strategy.getDestId());
            rank.setDestName(strategy.getDestName());

            ranks.add(rank);
        }
        super.saveBatch(ranks);

    }
}
